package ext.vpde.cadvaulting.migration;

import java.io.BufferedReader;
import java.util.Calendar;
import java.util.Hashtable;

import ext.vpde.cadvaulting.util.WINDSQL;


/*
 * ValidateEPMDocs reads a file containing all the iterations and their metadata, of CAD Documents 
 * from Product Center and creates EPMDocument objects in Windchill.
 */

public class ValidateEPMDocs {

	public static void main ( String [] args ) {
		if (args.length != 3) {
			System.out.println("\nUsage: java.ext.vpde.cadvaulting.ValidateEPMDocs adminid adminpw loadfile");
		} else {
			// Login to windchill
			if (LoadUtil.loginRemote(args[0],args[1])) {
				System.out.println("BEGIN VALIDATE EPM DOCS: " + Calendar.getInstance().getTime());
				try {
					// open a connection to oracle and leave it open throughout this load
					WINDSQL.openConnection();
					
					// open the input file
					BufferedReader file = LoadUtil.openInputFile(args[2]);
					if (file != null) {
						// read each line in the input file until we get to the end
						String s = file.readLine();
						int lineNumber = 0;
						while (s!= null && s.length() > 0) {
							LoadUtil.printProgress(lineNumber++);
							// build a hashtable of all the columns in each line
							Hashtable ht = LoadUtil.processInputLine(s);
							// test to see if the document exists
							String CADName = ht.get("CADNAME").toString();
							String docVer  = ht.get("VERSION").toString();
							String docIter = ht.get("ITERATION").toString();
							if (!WINDSQL.documentExists(CADName,docVer,docIter)) {
								System.out.println(CADName + " " + docVer + "." + docIter + " DOES NOT EXIST");
								
							}
							s = file.readLine();
						}
					} else {
						System.out.println("UNABLE TO OPEN THE FILE: " + args[2]);
					}
					
					// we are done, so close the oracle connection
					WINDSQL.closeConnection();
				} catch (Exception e) { LoadUtil.printException("ValidateEPMDocs.main", e); }
				System.out.println("-END- VALIDATE EPM DOCS: " + Calendar.getInstance().getTime());
			}
		}
    }
}